$(document).ready(function () {

    if(window.oddBoswer){
        $('body').empty();
        $('body').css({overflow:'hidden',width:'100%',height:"auto"});
        $('body').append(
            '<div>' +
            '<img class="odder-showing" draggable="false" src="images/odderbg.jpg">' +
            '</div>');
        return;
    }

    var businessId = 592;
    var newsOffset = 1;
    var toScalePic = function (url) {
        return "http://cimg.visualbusiness.cn" + url.slice(34)+"/imglarge";
    }
    //获取顶部新闻信息
    $.ajax({
        url:window.location.protocol + "//luna.visualbusiness.cn/luna-web/api/article/byBusinessId?business_id=405&column_ids=" + businessId + "&offset=0&limit=1",
        async:true,crossDomain: true == !(document.all),
        success:function(result){
            if (result.code === "0") {
                //更新顶部新闻
                if(result.data){
                    var newsData = result.data.rows[0];
                    var thumbnail = newsData.abstract_pic;
                    var title = newsData.title?newsData.title:"";
                    var newsDesc = newsData.abstract_content?newsData.abstract_content:"";
                    var time = newsData.create_time.substring(0,10);
                    $("#news-hot-time").text(time);

                    if (newsDesc && newsDesc.length > 33) {
                        newsDesc = newsDesc.substring(0, 30) + "...";
                    }
                    $("#news-hot-desc").text(newsDesc);

                    if (title && title.length > 13) {
                        title = title.substring(0,12) + "...";
                    }
                    $("#news-hot-title").text(title);

                    if (thumbnail.length && thumbnail.length > 1) {
                        $("#news-hot-picture").attr('src', thumbnail);
                        $("#news-top-phone-pic").hide();
                    }

                    var newsId = newsData.url.replace(/[^0-9]/ig,"");
                    $("#news-hot").bind('click', function () {
                        //获取新闻详情
                        var url = window.location.protocol + "//luna.visualbusiness.cn/luna-web/api/article/" + newsId;
                        $.ajax({
                            url:url,
                            async:true,crossDomain: true == !(document.all),
                            success:function(result){
                                console.log(result)
                                if (result.code === "0") {
                                    showNewsDetail(result.data);
                                }
                            }});
                    });

                }
            }
        }});

    getData(businessId, newsOffset);
    $(".text-left1").hover(function () {
        this.src="images/news/news/news-hot-learn-more2.png";
    },function(){
        this.src="images/news/news/news-hot-learn-more.png";
    });
    function updateNews(data) {
        var news = data.rows;
        for(var i=0; i<news.length;i++){
            var title = news[i].title;
            var desc = news[i].abstract_content;
            if (title.length>15) {
                title = title.substring(0,17) + "...";
            }
            if (desc.length>45) {
                desc = desc.substring(0,42) + "...";
            }

            $(".news-item").eq(i).find(".news-item-pic").attr('src', news[i].thumbnail?news[i].thumbnail:news[i].abstract_pic);
            $(".news-item").eq(i).find(".news-item-title").text(title);
            $(".news-item").eq(i).find(".news-item-time").text(news[i].publish_time.substring(0,10));
            $(".news-item").eq(i).find(".news-item-desc").text(desc);
            $(".news-item")[i].newsId = news[i].url.replace(/[^0-9]/ig,"");
            $(".news-item").eq(i).click(function () {
                //获取新闻详情
                var url = window.location.protocol + "//luna.visualbusiness.cn/luna-web/api/article/" + this.newsId;
                $.ajax({
                    url:url,
                    async:true,crossDomain: true == !(document.all),
                    success:function(result){
                        console.log(result)
                        if (result.code === "0") {
                            showNewsDetail(result.data);
                        }
                    }});
            });
        }
        $(".news-item").css({"display" : "inline"});
        if (news.length <= 6) {
            for(var i= news.length; i<7; i++) {
                $(".news-item").eq(i).css({"display" : "none"});
            }
        }
    }

    function getData(businessId, offset) {
        $.ajax({
            url:window.location.protocol + "//luna.visualbusiness.cn/luna-web/api/article/byBusinessId?business_id=405&column_ids=" + businessId + "&offset=" + offset + "&limit=6",
            async:true,crossDomain: true == !(document.all),
            success:function(result){
                if (result.code === "0") {
                    updateNews(result.data);
                    updateNavigation(result.data.total, offset);
                }
            }});
    }
    function updateNavigation(total, index) {
        $(".pagination").empty();
        var pageSize = (total-1)/6 + 1;
        if (total <= 0) {
            return;
        }
        $(".pagination").append('<li>'+
            '<span aria-hidden="true">&lt;</span>' +
            '</li>');
        for (var i=1; i<pageSize; i++) {
            $(".pagination").append('<li>'+
                '<span aria-hidden="true">'+ i+ '</span>' +
                '</li>');
            $(".pagination").children("li")[i].index = i;
            $(".pagination").children("li").eq(i).click(function () {
                var index = this.index - 1;
                getData(businessId, index*6 + 1);
            });
        }
        $(".pagination").append('<li>'+
            '<span aria-hidden="true">&gt;</span>' +
            '</li>');
        $(".pagination").children("li").eq(Math.floor(index/6) + 1).children("span").css({color:"#D7533C"});
    }

    function showNewsDetail(data) {
        $(".iframe-mon").css({display:"inline"});
        window.scrollTo(0, 0);
        $('section').append(
            '<article id="popwindow">' +
            '<div class="news-detail-window">' +
            '<div class="news-detail-contanier">' +
            '<img id="news-cancel" src="images/icon/cancel2.png">' +
            '<div id="detail-title">国际创新创业博览会</div>' +
            '<div id="detail-time">2017-03-31</div>' +
            '<div class="news-detail"></div>' +
            '<div class="text-center news-detail-back-top">返回顶部</div>' +
            '   </div>' +
            '</article>'
        );
        $(".news-detail").html(data.content);
        $(".news-detail").find('img').css({width:"100%"});
        $("#detail-title").text(data.title);
        $("#detail-time").text(data.publish_time.substring(0,10));
        $("#news-cancel").bind('click', function () {
            $('#popwindow').remove()
            $(".iframe-mon").css({display:"none"});
        });
        $(".news-detail-back-top").bind('click', function () {
            window.scrollTo(0, 0);
            $('#popwindow').scrollTop(0);
        });
    }
});